var express = require('express');
var router = express.Router();
var request = require('request');
var md5 = require('md5');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);

// 获取用户账户余额和消费记录
router.get('/jkyx/pc/get_balance', function(req, res, next) {
	var param = req.query || req.params; 
	// 手机
	var account = param.account || 0;

    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_get_balance,[account,account],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '登录成功',
	     			status:'100',
	     			results: results
	     		}));
			}
		})
	})
});
// 修改用户账户余额和消费记录
router.get('/jkyx/pc/change_balance', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户ID
	var account_id = param.account_id || 0;
	// 变化值
	var amount = param.amount || 0;
	// 账户余额
	var balance = param.balance || 0;
	// 事件
	var create_time = CommonJS.getNowFormatDate();
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_change_balance,[balance,account_id,account_id,amount,create_time],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));
			}
		})
	})
});

module.exports = router;
